package com.evil.kingdom.pojo.entity.action;

import com.evil.kingdom.utils.DijkstraUtil;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.Getter;
import lombok.Setter;
import org.springframework.validation.annotation.Validated;

import java.util.LinkedList;

/**
 * 路径点
 *
 * @author liyang
 * @date 2024-05-24 17:45
 */
@Getter
@Setter
public class RoutePosition {

    /**
     * 出发点
     */
    @NotBlank(message = "出发点不能为空")
    private final String departName;

    /**
     * 目的点
     */
    @NotBlank(message = "目的点不能为空")
    private final String destName;

    /**
     * 路径点
     */
    @NotEmpty(message = "目的点不能为空")
    private final LinkedList<ActionPosition> positions;

    /**
     * 权重
     */
    @NotNull(message = "权重不能为空")
    private final Double weight;

    public RoutePosition(@NotNull(message = "路径点导航本不能为空") @Validated Vector vector, boolean sign) {
        this.departName = sign ? vector.getDepartName() : vector.getDestName();
        this.destName = sign ? vector.getDestName() : vector.getDepartName();
        this.positions = sign ? vector.getPositions() : vector.newReversePosition();
        this.weight = DijkstraUtil.calWeight(vector.getPositions());
    }

    /**
     * 是否相同出发点
     *
     * @param departName departName
     * @return boolean
     */
    public boolean sameDepart(String departName) {
        return this.departName.equals(departName);
    }

    /**
     * 是否相同出发点
     *
     * @param destName destName
     * @return boolean
     */
    public boolean sameDest(String destName) {
        return this.destName.equals(destName);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        RoutePosition that = (RoutePosition) o;

        if (!departName.equals(that.departName)) return false;
        return destName.equals(that.destName);
    }

    @Override
    public int hashCode() {
        int result = departName.hashCode();
        result = 31 * result + destName.hashCode();
        return result;
    }

    public String printInfo() {
        return Vector.calVectorName(this.departName, this.destName);
    }
}
